<!DOCTYPE html>
<html lang="en">
<head>
    <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <meta charset="UTF-8">
    <title>明平强测试</title>
</head>
<body>

<shiro:hasRole name="customer">
<!--<h1>欢迎光临，梅校玉!请选择你的操作:</h1><br>-->
<!--</shiro:hasRole>-->
<!--<ul>-->
    <!--<shiro:hasPermission name="add"><li>增加</li></shiro:hasPermission>-->
    <!--<shiro:hasPermission name="delete"><li>删除</li></shiro:hasPermission>-->
    <!--<shiro:hasPermission name="update"><li>修改</li></shiro:hasPermission>-->
    <!--<shiro:hasPermission name="query"><li>查询</li></shiro:hasPermission>-->
<!--</ul>-->
<shiro:hasRole name="admin">
    <h1>欢迎光临，明平强!请选择你的操作:</h1><br>
</shiro:hasRole>
<button onclick="submit()">点我注销登陆</button>
<form id="orderForms">
<div class="form-group">
    <h1>上传背景图</h1>
<input type="file"  name="file" id="file" onchange="upload()"/>
<input type="button" class="btn btn-warning" onclick="addFile()" value="提交"/>
</div>
</form>
</body>
<script type="application/javascript">
    function submit() {
        $.ajax({
            type: "POST",
            url: "/mpq/logOut",
            success: function(data){
                self.location="/mpq/login.html"
            }
        });
    }
    var filePath ="";
    var fileType ="";
    var fileName2 ="";
    function upload(){
        var fileName = $('#file').val().split('\\'); //得到文件名数组
        var fileSize =  document.getElementById('file').files[0]; //获得文件大小；
        fileName2 = fileName[fileName.length-1]; // 获得文件名
        filePath = $('#file').val().toLowerCase().split(".");
        fileType =  filePath[filePath.length - 1]; //获得文件结尾的类型如 zip rar 这种写法确保是最后的
        $('.errHint').show().text(fileName[2]);
        console.log(filePath);
        console.log(fileType);
         if(!(fileType == "png" || fileType == "jpg")){
            alert('文件格式不符合要求！')
            return false;
        }else
        /* if(!(fileType == "png" || fileType == "jpg" || fileType == "xlsx"|| fileType == "docx"|| fileType == "rar" || fileType == "zip" )){
             alert('文件格式不符合要求！')
             return false;
         }else*/ if(fileSize.size>104857600){
            alert('错误！请上传不超过100M的文件');
            return false;
        }
        return true;
    }
    function addFile() {
        var boo = upload();
        if (boo == false){
            return;
        }
        var file = $("input[name='file']").val();
        if(file === "" || file ==null){
            alert("文件为空");
            return;
        }
        var formData = new FormData(document.getElementById("orderForms"));//表单id
        var boo = false;
        var str = "";
        $.ajax({
            url: "/mpq/file/upload",
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (result) {
                boo = result.code === 0;
                str = result.msg;
            }
        })
        if (boo) {
            alert('操作成功');
        } else {
            alert(str);
        }
    }
</script>
</html>